<template>
  <div class="home">
    <el-form label-width="100px">
      <el-form-item label="商品编码">
        <el-input v-model="commodityCode"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="commodityName"></el-input>
      </el-form-item>
      <el-form-item label="商品分类">
        <el-cascader ref="prodType" v-model="prodType" :options="dict" @change="typeChange"></el-cascader>
      </el-form-item>
      <el-form-item label="商品一级规格">
        <el-select v-model="prodTypeOne" placeholder="请选择">
          <el-option v-for="item in typeOne" :key="item.value" :label="item.label" :value="item.value+':'+item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品二级规格">
        <el-select v-model="prodTypeTwo" placeholder="请选择">
          <el-option v-for="item in typeTwo" :key="item.value" :label="item.label" :value="item.value+':'+item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生产企业名称">
        <el-input v-model="productionFactoryName"></el-input>
      </el-form-item>
      <el-form-item label="生产企业地址">
        <el-cascader ref="addr" v-model="address" :options="position" @change="handleChange"></el-cascader>
      </el-form-item>

      <el-form-item label="图片">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// @ is an alias to /src
import { upload, addProd } from '../api/server'
import POSITION from '../common/position.json'
import DICT from '../common/dict.json'
import TYPEONE from '../common/typeOne.json'
import TYPETWO from '../common/typeTwo.json'

export default {
  name: 'home',
  data() {
    return {
      form: {},
      position: POSITION.res,
      address: '',
      addressLabel: {},
      dict: DICT.res,
      typeOne: TYPEONE.res,
      typeTwo: TYPETWO.res,
      prodType: '',
      prodTypeOne: '',
      prodTypeTwo: '',
      imageUrl: '',
      commodityName: '',
      prodTypeLabel: {},
      productionFactoryName: '',
      commodityCode: ''
    }
  },
  mounted() {
  },
  methods: {
    onSubmit() {
      let prodTypeLabel = this.prodTypeLabel.presentText.split('/')
      let specsOne = this.prodTypeOne.split(':')
      let specsTwo = this.prodTypeTwo.split(':')
      let addressLabel = this.addressLabel.presentText.split('/')
      let form = {
        commodityCode: this.commodityCode,
        commodityName: this.commodityName,
        commodityOneType: this.prodType[0],
        commodityOneTypeName: prodTypeLabel[0].trim(),
        commodityTwoType: this.prodType[1],
        commodityTwoTypeName: prodTypeLabel[1].trim(),
        specsOneId: specsOne[0],
        specsOneName: specsOne[1],
        specsTwoId: specsTwo[0],
        specsTwoName: specsTwo[1],
        productionFactoryName: this.productionFactoryName,
        provinceId: this.address[0],
        province: addressLabel[0].trim(),
        cityId: this.address[1],
        city: addressLabel[1].trim(),
        countyId: this.address[2],
        county: addressLabel[2].trim(),
        commodityImg: this.imageUrl
      }
      addProd(form).then(res => {
        this.$message.success('上传成功')
        this.commodityName = ''
        this.prodType = ''
        this.prodTypeLabel = {}
        this.prodTypeOne = ''
        this.prodTypeTwo = ''
        this.address = ''
        this.addressLabel = {}
        this.commodityCode = ''
        this.productionFactoryName = ''
        this.imageUrl = ''
      })

    },
    handleChange(val) {
      this.addressLabel = this.$refs['addr']._data
    },
    beforeAvatarUpload(file) {
      let form = new FormData()
      form.append('file', file)
      upload(form).then(res => {
        this.imageUrl = res.res
      })
      return false
    },
    handleAvatarSuccess() {

    },
    typeChange() {
      this.$nextTick(() => {
        this.prodTypeLabel = this.$refs['prodType']._data
      })
    }
  }
}
</script>
<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

